package own.stu.jobgib.playown.vediodown.bili.service;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import own.stu.jobgib.playown.vediodown.bili.model.UserInfo;
import own.stu.jobgib.playown.vediodown.common.BaseModel;
import own.stu.jobgib.playown.vediodown.common.config.DefaultConfig;

import java.io.IOException;
import java.util.concurrent.Future;

public class UserService {

    private static Logger logger = LoggerFactory.getLogger(UserService.class);

    private AsyncHttpClient client;
    private Gson gson;

    public UserService(DefaultConfig cfg) {
        client = cfg.getClient();
        gson = cfg.getGson();
    }

    static String userInfoUrl = "https://api.bilibili.com/x/space/wbi/acc/info?mid=%s";

    private static String getUserInfoUrl(String userId) {
        return String.format(userInfoUrl, userId);
    }

    public UserInfo getUserInfo(String userId) {
        Future<Response> f = client.prepareGet(getUserInfoUrl(userId))
                .addHeader("Cookie", "buvid3=91F9E608-C8BA-21CA-EE39-26D9B14EB07434734infoc; b_nut=1678351134; _uuid=9BD681410-2FE5-AFB5-710103-B6FBB94D414135033infoc; buvid4=ABEEBA7A-252A-8AA0-C872-3AF9E88A18B836074-023030916-jMUoPae2vejbN4MALaSPgw%3D%3D; rpdid=|(J|J|luYJYl0J'uY~)Jl~)mu; nostalgia_conf=-1; hit-dyn-v2=1; LIVE_BUVID=AUTO2216784469803026; i-wanna-go-back=-1; b_ut=5; buvid_fp_plain=undefined; CURRENT_PID=e75c4fa0-cd14-11ed-a7d2-a7828328f196; FEED_LIVE_VERSION=V8; hit-new-style-dyn=1; iflogin_when_web_push=1; go-back-dyn=1; CURRENT_BLACKGAP=0; enable_web_push=ENABLE; header_theme_version=CLOSE; is-2022-channel=1; DedeUserID=384008796; DedeUserID__ckMd5=be6ec5898f138ed9; CURRENT_QUALITY=80; browser_resolution=1299-725; home_feed_column=4; fingerprint=1b3e7659619970f2ef14aa972b5c206a; CURRENT_FNVAL=4048; buvid_fp=1b3e7659619970f2ef14aa972b5c206a; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDI5ODY1NzAsImlhdCI6MTcwMjcyNzMxMCwicGx0IjotMX0.-7YtoJxsvx8Gf2jW3mkFhjIStcMLDMf255qcRTeiF4k; bili_ticket_expires=1702986510; bsource=search_baidu; PVID=3; bp_video_offset_384008796=876108912467640354; innersign=0; b_lsid=F310FC455_18C7A99EDB5; SESSDATA=c7324bf2%2C1718417708%2C20abc%2Ac2CjCeJRYJCbK4UjrXeEhQbYPI0LL4YuaGxraH32mz-_7plMixxuoOIxEWyZ5y8P3Ov0oSVjFLMWF4TFhUSnhRMllNUklIYTF4RUdTVWRSVVdqTUhSZ3pQNkNmbkgxdURydUxTWVdPeTI5TWdKYXhBSzdUel8xWlY0WE9iNk5qMGZfX3lyTTRFNWFRIIEC; bili_jct=1bc368afc0851f9682dd61a369a30b1c; sid=7dky1jp3")
                .execute();
        Response response = null;
        try {
            response = f.get();
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        String responseBody = response.getResponseBody();
        if (StringUtils.isBlank(responseBody)) {
            logger.info("userId {} is null", userId);
            return null;
        }
        BaseModel<UserInfo> model = gson.fromJson(responseBody, new TypeToken<BaseModel<UserInfo>>() {
        }.getType());
        return model.getData();
    }

    public static void main(String[] args) throws IOException {
        DefaultConfig cfg = new DefaultConfig();
        UserService userService = new UserService(cfg);
        UserInfo userInfo = userService.getUserInfo("3493140465978044");
        System.out.println(userInfo);
        cfg.destory();
    }
}
